package com.netdisk.controller;
/**
 * @author chenlong
 * 上传文件的aciotn
 * 2013-5-17 22:49:56
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import static com.netdisk.util.RemoteAddress.getRemoteAddress;
import static com.netdisk.util.UUIDCreator.getUuId;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netdisk.entity.Capacity;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.impl.CapacityService;
import com.netdisk.service.impl.FileSystemService;
import com.netdisk.service.impl.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("uploadFileAction")
@Scope("prototype")
public class UploadFileAction extends ActionSupport implements ModelDriven{
	private FileSystemService fileSystemService;
	
	private CapacityService capacityService;
	
	@Resource(name="capacityService")
	public void setCapacityService(CapacityService capacityService) {
		this.capacityService = capacityService;
	}
	
	@Resource(name="fileSystemService")
	public void setFileSystemService(FileSystemService fileSystemService) {
		this.fileSystemService = fileSystemService;
	}
	
	// 附加表单字段, 获取当前传文件的父路径
	private String parentDir;
	
	// 附加表单字段, 获取当前传文件的路径
	private String currentDir;
	
	public String getParentDir() {
		return parentDir;
	}

	public void setParentDir(String parentDir) {
		this.parentDir = parentDir;
	}

	public String getCurrentDir() {
		return currentDir;
	}

	public void setCurrentDir(String currentDir) {
		this.currentDir = currentDir;
	}
	// 文件保存的路径
	private String path;
	
	// 文件的实际上传名称
	private String fileFileName; 
	
	// 文件的file对象
	private File file;
	
	// 缓存路径下的对应文件名
	private String Filename;
	
	private String Upload;
	
	//文件的类型, 这里暂时不用
	private List<String> fileContentType;
		
	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFilename() {
		return Filename;
	}

	public void setFilename(String filename) {
		Filename = filename;
	}

	public String getUpload() {
		return Upload;
	}

	public void setUpload(String upload) {
		Upload = upload;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Object getModel() {
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public String getPath() throws Exception {
		return ServletActionContext.getRequest().getRealPath(path);
	}
	private String getStoreName(String fileName, String storeName){ //存在在数据里面的名字 uuid
		int position = fileName.lastIndexOf(".");
		String extension = fileName.substring(position);
		return storeName + extension;
	}

	public String execute() throws Exception {
		System.out.println(this.toString());
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if (null == session.getAttribute("user")){ // 非法的session
			return ERROR;
		}
		
		User user = (User)session.getAttribute("user"); //得到当前用户
		
		Capacity cap =  capacityService.findByOwnerId(user.getId());
		
		// 用户上传的文件超过允许的最大容量
		System.out.println(cap);
		
		if(cap.getAlreadyUse() + file.length() > cap.getMaxCapacity()){
			System.out.println(file.length());
			return null;
		}
		
		
		String logined =(String)session.getAttribute("logined");
		
		String fileStoreName = getUuId();
		
		FileOutputStream fos = new FileOutputStream(getPath() + File.separator +
				currentDir + File.separator +  fileStoreName);
		
		FileInputStream fis = new FileInputStream(file);
		byte[] b = new byte[2000 * 1024];
		int length = 0;
		while ((length = fis.read(b)) > 0) {// 文件写入文件系统
			fos.write(b, 0, length);
		}
		
		String ip = getRemoteAddress(request);
		
		FileSystem fileSystem = new FileSystem(getUuId(), user.getCompanyId(),user.getCompanyName(), currentDir, fileStoreName,
				(byte)0, fileFileName, file.length(), Calendar.getInstance().getTimeInMillis(), 
				Calendar.getInstance().getTimeInMillis(), user.getId(), user.getLoginName(), (byte)1, 
				740, (byte)0, ""); 
		Log log = new Log(getUuId(), user.getCompanyId(), user.getCompanyName(), 
				user.getId(), user.getLoginName(), (byte)32, Calendar.getInstance().getTimeInMillis(), ip);
		cap.setAlreadyUse(cap.getAlreadyUse() + file.length());
		fileSystemService.save(fileSystem, cap, log);
		
		fos.close();
		fis.close();
	    return null;
	}

	@Override
	public String toString() {
		return "UploadFileAction [parentDir=" + parentDir + ", currentDir="
				+ currentDir + ", path=" + path + ", fileFileName="
				+ fileFileName + ", file=" + file + ", Filename=" + Filename
				+ ", Upload=" + Upload + ", fileContentType=" + fileContentType
				+ "]";
	}
	
}
